//
//  BanBu_FriendListWebViewController.m
//  BanBu
//
//  Created by Wang Xj on 13-9-13.
//
//

#import "BanBu_FriendListWebViewController.h"
#import "BanBu_FriendListCell.h"
#import "AppCommunicationManager.h"
#import "AppDataManager.h"
#import "BanBu_NavButton.h"
#import "BanBu_ThemeManager.h"
#import "BanBu_WebFrame_ViewController.h"

@interface BanBu_FriendListWebViewController ()

@end

@implementation BanBu_FriendListWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.6];
    self.title = NSLocalizedString(@"choosePerson", nil);
    
    self.friendListArray = [NSMutableArray arrayWithCapacity:30];
    self.userInfoDic = [NSDictionary dictionary];
    
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 290, __MainScreen_Height-30)];
    bgView.backgroundColor = [UIColor colorWithRed:1 green:250.0/255 blue:240.0/255 alpha:1];
    bgView.layer.cornerRadius = 10;
    
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[ThemeManager getThemeImage:@"topbar.png"]];
    imageView.frame = CGRectMake(0, 0, 290, 44);
    [bgView addSubview:imageView];
    [imageView release];
    
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, __MainScreen_Width, 44)];
    headerView.backgroundColor = [UIColor clearColor];
    
    CGFloat btnLen1 = [NSLocalizedString(@"choose", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(150, 30)].width;
    self.chooseButton = [[[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal] autorelease];
    self.chooseButton.frame=CGRectMake(10, 10, btnLen1+20, 30);
    self.chooseButton.tag = 101;
    [self.chooseButton addTarget:self action:@selector(setEditing:animated:) forControlEvents:UIControlEventTouchUpInside];
    [self.chooseButton setTitleEdgeInsets:UIEdgeInsetsMake(3, 2, 2, 2)];
    [self.chooseButton setTitle:NSLocalizedString(@"choose", nil) forState:UIControlStateNormal];
    self.chooseButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [headerView addSubview:self.chooseButton];
    
    BanBu_NavButton * cancelButton = [[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal];
    cancelButton.frame=CGRectMake(10, 10, btnLen1+20, 30);
    cancelButton.tag = 102;
    [cancelButton addTarget:self action:@selector(cancelChoose:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitleEdgeInsets:UIEdgeInsetsMake(3, 2, 2, 2)];
    [cancelButton setTitle:NSLocalizedString(@"cancelNotice", nil) forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [headerView addSubview:cancelButton];
    
    [bgView addSubview:headerView];
    [headerView release];
    
    self.FriendtableView = [[[UITableView alloc]init] autorelease];
    self.FriendtableView.frame = CGRectMake(0, 44, 290, __MainScreen_Height-30-44);
    self.FriendtableView.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:240.0/255 alpha:1.0];
    self.FriendtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.FriendtableView.dataSource = self;
    self.FriendtableView.delegate = self;
    self.FriendtableView.allowsSelectionDuringEditing = YES;
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    self.FriendtableView.backgroundView = img;
    [bgView addSubview:self.FriendtableView];
    
    [self.view addSubview:bgView];
    [bgView release];
       
    NSString * fileName = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"friendList_Transmit"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:fileName])
    {
        //获取好友列表
        NSDictionary * friendListDic = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
        
        NSDate * updateDate = [friendListDic valueForKey:@"updateDate"];
        self.friendListArray = [friendListDic valueForKey:FriendShip];
        
        if (fabs([updateDate timeIntervalSinceNow])>86400)
        {
            //获取好友列表
            [AppComManager getBanBuData:BanBu_GetFriendList_transmit par:nil delegate:MyAppDataManager];
        }
    }
    else
    {
        [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"isDownloading", nil) activityAnimated:YES];
        //获取好友列表
        [AppComManager getBanBuData:BanBu_GetFriendList_transmit par:nil delegate:MyAppDataManager];
    }
    
    
    
    [self setEditing:YES animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dismissTKLoading
{
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:YES afterShow:1.5];
}

-(void)cancelChoose:(UIButton *)sender
{
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:YES afterShow:0];
    [self.view removeFromSuperview];
}

//修改tableView可编辑状态
- (void)setEditing:(BOOL)editting animated:(BOOL)animated
{
    [self.FriendtableView setEditing:!self.FriendtableView.editing animated:YES];
	[super setEditing:!self.editing animated:YES];
    
    if(self.editing)
    {
        //为了使用多语言环境，改变文本显示时，要调整按钮长度
        CGFloat btnLen1 = [NSLocalizedString(@"finishButton", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(150, 30)].width;
        self.chooseButton.frame=CGRectMake(290-btnLen1-30, 10, btnLen1+20, 30);

        [self.chooseButton setTitle:NSLocalizedString(@"finishButton", nil) forState:UIControlStateNormal];
    }
    else
    {
        //为了使用多语言环境，改变文本显示时，要调整按钮长度
        CGFloat btnLen1 = [NSLocalizedString(@"choose", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(150, 30)].width;
        self.chooseButton.frame=CGRectMake(290-btnLen1-30, 10, btnLen1+20, 30);
        
        [self.chooseButton setTitle:NSLocalizedString(@"choose", nil) forState:UIControlStateNormal];
    }
    if(self.userInfoDic.count)
    {
        [self.view removeFromSuperview];
        [self.web getSelectedUserInfo:self.userInfoDic];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.friendListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BanBu_FriendListCell *cell = (BanBu_FriendListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[BanBu_FriendListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    NSDictionary * friendInfoDic = [self.friendListArray objectAtIndex:indexPath.row];
    
    [cell setAvatar:[friendInfoDic valueForKey:KeyUface]];
    [cell setName:[friendInfoDic valueForKey:KeyPname]];
    
    
    if ((indexPath.row+1) == self.chooseRow)
    {
        [cell setChecked:YES];
    }
    else
    {
        [cell setChecked:NO];
    }
    
    return cell;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

#pragma mark - Table view delegate
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(self.FriendtableView.editing)
    {        
        BOOL isChecked = YES ;
        
        if (self.chooseRow!=(indexPath.row+1))
        {
            isChecked = NO;
        }
        
        BanBu_FriendListCell *cell = (BanBu_FriendListCell *)[tableView cellForRowAtIndexPath:indexPath];
        
        [cell setChecked:!isChecked];
        
        if(!isChecked)
        {
            if (self.aIndexPath)
            {
                BanBu_FriendListCell *cell = (BanBu_FriendListCell *)[tableView cellForRowAtIndexPath:self.aIndexPath];
                [cell setChecked:isChecked];
            }
            
            self.aIndexPath = indexPath;
            self.chooseRow = indexPath.row + 1;
            self.userInfoDic = [self.friendListArray objectAtIndex:indexPath.row];
        }
        else
        {
            self.aIndexPath = nil;
            self.chooseRow = 0;
            self.userInfoDic = [NSMutableDictionary dictionary];
        }
    }
}

-(void)dealloc
{
    [_userInfoDic release];
    [_chooseButton release];
    [_FriendtableView release];
    [_friendListArray release];
    
    [super dealloc];
}

@end;